package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.ImageActivity;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.view.SquareLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * GridView列表适配器 特殊处理(1张一行显示一个长方形图、2张一行展示两个等宽高图、3张及以上一行展示三张等宽高图
 * Ps:显示等宽高图片是更多GridView设置的列数来确定的)
 *
 * @author tangkun
 */
public class ImageAdapter extends BaseAdapter {

    private Context context;

    private List<String> urlList = new ArrayList<String>();

    public ImageAdapter(Context context, List<String> urlList) {
        this.context = context;
        this.urlList = urlList;
    }


    @Override
    public int getCount() {
        return urlList == null ? 0 : urlList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return urlList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    class ViewHolder {
        SquareLayout moreThanOneSl;
        public ImageView imageView;
        private ImageView justOnePicIv;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_common_image, null);
            holder = new ViewHolder();
            holder.moreThanOneSl = (SquareLayout) convertView.findViewById(R.id.sl_more_than_one);
            holder.imageView = (ImageView) convertView.findViewById(R.id.image);
            holder.justOnePicIv = (ImageView) convertView.findViewById(R.id.iv_just_one);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (urlList != null) {
            if (urlList.size() == 1) {
                holder.justOnePicIv.setVisibility(View.VISIBLE);
                holder.moreThanOneSl.setVisibility(View.GONE);
                //图片
                Glide.with(context)
                        .load(GlobalUrl.IMG_URL + urlList.get(position))
                        .dontAnimate()
                        .placeholder(R.mipmap.ic_place_holder)
                        .into(holder.justOnePicIv);
            } else {
                holder.justOnePicIv.setVisibility(View.GONE);
                holder.moreThanOneSl.setVisibility(View.VISIBLE);
                //图片
                Glide.with(context)
                        .load(GlobalUrl.IMG_URL + urlList.get(position))
                        .dontAnimate()
                        .placeholder(R.mipmap.ic_place_holder)
                        .into(holder.imageView);
            }
            holder.imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ImageActivity.class);
                    intent.putExtra("urlList", (Serializable) urlList);
                    intent.putExtra("index", position);
                    context.startActivity(intent);
                }
            });
            holder.justOnePicIv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ImageActivity.class);
                    intent.putExtra("urlList", (Serializable) urlList);
                    intent.putExtra("index", position);
                    context.startActivity(intent);
                }
            });
        }

        return convertView;
    }

}
